package x10.utilities;

/**
 * CheckCode - Utility class for calculating check codes,
 * CRC codes and any other error checking methods needed to validate
 * or verify received X10 data
 */

public class CheckCode {

    /**
     *  A static method for calculating the check code for an X10 RF Remote
     *  Control command.
     *
     *  X10 RF Remote Check Code
     *  Example: 0x5D 0x14 0x37 0x02 0x60 (Power/Standby, house code A)
     *      5D = CM15A Prefix for RF Code
     *      14 = Header identifier for Remote control codes/commands
     *      37 = Check code
     *      02 = Remote code
     *      60 = Address code (House code A = 6)
     *
     *  Algorithm: Check Code = (0xD4 + Remote Code + House Code) % 0xFF
     *      (% = modulus)
     *
     *  @param remoteCode - byte value of the remote control code
     *  @param addressCode - byte value of house code in upper nibble with zero
     *                       in lower nibble.  Example 0x60 (house code A = 6)
     *  @return checkCode - byte value of the check code
     *  @author Denny Sheirer
     *  @date   20110124
     */

    public static byte getRemoteCheckCode(byte remoteCode, byte addressCode){
        //Algorithm:
        int val = (0xD4 + remoteCode + addressCode) % 0xFF;

        //Cast it to a byte value for return
        return (byte)(val & 0xFF);
    }
}
